﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinApp.ViewModels.Base;
using FinApp.Commands.Base;
using Microsoft.Phone.Controls;
using FinApp.DataProviders;
using FinApp.Commands;
using FinApp.DataProviders.EventArguments;
using FinApp.DataProviders.Enumerations;

namespace FinApp.ViewModels {
    public class LoginViewModel : ViewModelBase {

        #region Members
        private string userName = "treemod40"; /// TODO: dejar en blanco
        private string userPassword = "treemod40";
        private LoginCommand signInCommand;
        #endregion

        #region Constructors
        public LoginViewModel() {
            this.signInCommand = new LoginCommand(() => {
                LoginProvider.Instance.LoginCompleted += new DataProviders.EventArguments.LoginCompleteEventHandler(OnLoginCompleteEventHandler);
                LoginProvider.Instance.DoLoginAsync(this.UserName, this.UserPassword);
            });

        }
        #endregion

        #region Properties
        /// <summary>
        /// Obtiene o establece el nombre de usuario
        /// </summary>
        /// <returns></returns>
        public string UserName {
            get { return this.userName; }
            set {
                if (value != this.userName) {
                    this.userName = value;
                    this.signInCommand.SetUserName(this.userName);
                    this.NotifyPropertyChanged("UserName");
                }
            }
        }

        /// <summary>
        /// Obtiene o establece el password del usuario
        /// </summary>
        /// <returns></returns>
        public string UserPassword {
            get { return this.userPassword; }
            set {
                if (value != this.userPassword) {
                    this.userPassword = value;
                    this.signInCommand.SetUserPassword(this.userPassword);
                    this.NotifyPropertyChanged("Password");
                }
            }
        }
        /// <summary>
        /// Obtiene el comando de ejecución de login
        /// </summary>
        public ICommand SignInCommand {
            get { return signInCommand; }
        }
        #endregion

        #region Events

        private void OnLoginCompleteEventHandler(object sender, LoginEventArgs e) {

            switch (e.LoginStatus) {
                case ProviderResponseEnumeration.OK:
                    var res = App.Current.RootVisual as PhoneApplicationFrame;
                    res.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
                    break;
                case ProviderResponseEnumeration.Failed:

                    break;
            }

        }
        #endregion


        public override void LoadData() {
            throw new NotImplementedException();
        }
    }
}